package top.dbpanda.cmsjpa.learn.thread;

import java.util.concurrent.atomic.AtomicInteger;

/**
 * volatile 所有线程可见性
 * CAS算法
 *
 * @author 20018954
 */
public class TestVolatile {
    public static volatile int num = 0;
    public static AtomicInteger num2 = new AtomicInteger(0);

    public static void main(String[] args) throws InterruptedException {
        for (int i = 0; i < 100; i++) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    for (int j = 0; j < 20000; j++) {
                        num++;//非原子性
                        num2.incrementAndGet();
                    }
                }
            }).start();
        }
        Thread.sleep(3000);
        System.out.println(num);
        //原子打印
        System.out.println(num2);
    }
}
